package com.hrt.test.db;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.btcode.common.ExecuteTimeUtil;
import com.btcode.common.MyLog;
import com.btcode.common.PropertiesUnit;
import com.btcode.db.executer.IDataExecuter;
import com.hrt.db.DBManager;

public class LockDB extends Thread {

    public void run() {

        PropertiesUnit.setPropertiesPath("E:/workspace/DataSenderV2/DataSenderV2/resource/");
        MyLog.getInstance().setRootPath("d:/DataSender");

        System.out.println("开始执行");

        IDataExecuter dataExecuter = DBManager.getDataExecuter();

        try {
            dataExecuter.beginTransaction();
            SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
            dataExecuter.update("insert into BC_MONITOR_PARAM_DICT(id) values('" + sf.format(new Date()) + "')", null);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            System.out.println("开始休眠");
            Thread.sleep(1000 * 5);
        } catch (Exception e) {
            e.printStackTrace();
        }

        dataExecuter.commit();

        System.out.println("提交成功");
    }

    public static void main(String[] args) {
        LockDB ldb = new LockDB();
        ldb.start();
        ldb.updateDB();
    }

    public void updateDB() {
        System.out.println("开始查询");

        ExecuteTimeUtil time = new ExecuteTimeUtil();

        SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");

        PropertiesUnit.setPropertiesPath("E:/workspace/DataSenderV2/DataSenderV2/resource/");
        MyLog.getInstance().setRootPath("d:/DataSender");
        IDataExecuter dataExecuter = DBManager.getDataExecuter();

        System.out.println("获取数据库对象所花时间：" + time.getLastDiff());

        try {
            dataExecuter.beginTransaction();
            dataExecuter.update("PRAGMA busy_timeout = 100;", null);
            dataExecuter.update(
                    "update BC_MONITOR_PARAM_DICT_test set UNIT = '" + sf.format(new Date()) + "' where id='123'",
                    null);
            dataExecuter.commit();
        } catch (Exception e) {
            System.out.println("执行语句所花时间：" + time.getLastDiff());
            throw e;
        }

        System.out.println("执行语句所花时间：" + time.getLastDiff());
    }

}
